package com.jy.horny.function.home

import android.os.Handler
import android.util.Log
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.jy.horny.R
import com.jy.horny.adapter.HomeListAdapter
import com.jy.horny.base.BaseVMFragment
import com.lib.module_base.utils.LogUtil
import com.jy.horny.base.HttpRequestListener
import com.jy.horny.databinding.FragmentHomeBinding
import com.jy.horny.dialog.CustomPopup
import com.jy.horny.function.home.viewmodel.HomeViewModel
import com.jy.horny.model.HomeListModel
import com.lib.module_base.utils.SaveDeviceUtil
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener

class HomeFragment : BaseVMFragment<FragmentHomeBinding, HomeViewModel>(
    FragmentHomeBinding::inflate,
    HomeViewModel::class.java
) {

    private lateinit var homeListAdapter :HomeListAdapter

    override fun initView() {
        binding?.homeRefreshLayout?.setOnLoadMoreListener(object :OnLoadMoreListener{
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                network()
                binding!!.homeRefreshLayout.finishLoadMore(1000)
            }

        })
        binding!!.homeRecyclerView.layoutManager=LinearLayoutManager(activity)
        homeListAdapter = HomeListAdapter(activity)
        binding!!.homeRecyclerView.adapter=homeListAdapter
    }
    override fun initData() {
        network()
        checkLogOut()
    }

    private fun network() {
        mViewModel?.getData(object : HttpRequestListener<HomeListModel>() {
            override fun addRequestParams(params: HashMap<String, Any>) {
            }

            override fun addDynamicsHeadParams(dynamicsHeadParamMap: HashMap<String, Any>) {
            }

            override fun success(t: HomeListModel?) {
                binding?.emptyContainer?.emptyContainer?.isVisible=t?.homepageList == null
                binding?.homeRefreshLayout?.isVisible=t?.homepageList != null
                t?.homepageList?.apply {
                    homeListAdapter.getDataList().addAll(t.homepageList)
                    homeListAdapter.notifyDataSetChanged()
                }
            }

            override fun error(throwable: Throwable) {
                LogUtil.debugLog("11111", "Throwable")
            }
        })

    }

    fun checkLogOut(){
        mViewModel?.checkLogOut(object :HttpRequestListener<String>(){
            override fun addRequestParams(params: HashMap<String, Any>) {
                params["userCode"]=SaveDeviceUtil.getUserCode()?:""
            }

            override fun success(t: String?) {
                activity?.apply {
                    val dialog = CustomPopup(this)
                        .setLayoutId(R.layout.popup_user_regression)
                        .setControllerListener { dialog ->
                            dialog.findViewById<TextView>(R.id.popup_content).text = t
                        }
                        .setOnClickListener(R.id.popup_close){ dialog, _ ->
                            dialog.dismiss()
                        }
                        .show()
                    Handler().postDelayed(Runnable {
                        dialog.dismiss()
                    }, 3000)
                }

            }
        })
    }
}